/**
 * Created by json.lu on 2016/8/23.
 */
var mysql=require('../config/mysqldb')();
var when=require('when');
var newsModel={};

newsModel.insert=function(newsArr,callback)
{
    mysql.query('insert into news (title,des,time,origin,origin_detail_url) values (?,?,?,?,?)',newsArr,function(error,result){
        callback(error,result);
    })
}
newsModel.getAllUrl=function(callback)
{
    mysql.query('select id,origin_detail_url from news where content is null or content<>""',function(error,result){
        callback(error,result);
    })
}

newsModel.getList=function(params,callback)
{
    mysql.query('select * from news where title like "%'+params.search+'%" order by id desc limit '+params.offset+','+params.limit,function(error,result){
        mysql.query('select count(*) total from news where title like "%'+params.search+'%"',function(error,total){
            callback(error,result,total[0].total);
        })
    })
}

newsModel.getList_=function(params,callback)
{
    var start_page=parseInt(params.offset)*parseInt(params.limit);
    var where_arr=[1];
    /*if(params.search){
     where_arr.push('title like "%'+params.search+'%"')
     }*/
    mysql.query('select id,title,origin_detail_url,time from news where title like "%'+params.search+'%" order by id desc limit '+params.offset+','+params.limit,[start_page,params.limit],function(error,result){
        mysql.query('select count(*) total from news where title like "%'+params.search+'%"',function(error,total){
            callback(error,result,total[0].total);
        })
    })
}

newsModel.getDetail=function(id,callback)
{
    mysql.query('select * from news where id=?',[id],function(error,result){
        callback(error,result);
    })
}

newsModel.updateContent=function(id,content,callback)
{
    mysql.query('update news set content=? where id=?',[content,id],function(error,result){
        callback(error,result);
    })
}
module.exports = newsModel;